package com.rjkj.medicalwaste.ui.first

import android.Manifest
import android.content.*
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import com.rjkj.medicalwaste.base.BaseFragment
import com.rjkj.medicalwaste.databinding.FragmentUploadBinding
import com.rjkj.medicalwaste.download.DownLoadProgress


class UploadFragment : BaseFragment<FragmentUploadBinding>() {
    val registerForActivityResult =
        registerForActivityResult(ActivityResultContracts.RequestPermission()) {
            if (it) {
                val progress = DownLoadProgress(requireContext())
                val url = "https://www.rjzhyl.com/cartAPK/phone2/MedicalWaste.apk"
                val fileName = url.split("/").last()
                progress.showProgress(url, fileName, fileName)
            }

        }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        val binding = FragmentUploadBinding.inflate(inflater, container, false)
        onSubscribeUi(binding)
        binding.lifecycleOwner = viewLifecycleOwner
        return binding.root
    }


    override fun onSubscribeUi(binding: FragmentUploadBinding) {

        binding.btStart.setOnClickListener {
            registerForActivityResult.launch(Manifest.permission.WRITE_EXTERNAL_STORAGE)
        }
//        binding.btStart.setOnClickListener{
//            lifecycleScope.launch(IO) {
//                val response = Api.downloadFile("https://www.rjzhyl.com/cartAPK/phone2/MedicalWaste.apk")
//                dowload(response) {
//                    success { uri ->
//                        //下载成功 uri:下载文件的uri
//                        //uri转file,可以参考：https://blog.csdn.net/jingzz1/article/details/106188462
//                    }
//                    error { throwable ->
//                        //下载失败
//                    }
//                    process { downloadedSize, length, process ->
//                        //下载进度
//                        //downloadedSize:已下载的大小
//                        //length:文件总大小
//                        //process:下载进度
//                        Log.e(TAG, "downloadedSize: $downloadedSize")
//                        Log.e(TAG, "length: $length")
//                        Log.e(TAG, "process: $process")
//                        binding.progress.progress = (process * 100).toInt()
//                    }
//                    //设置文件名，如果不设置，会自动获取
//                    setFileName = { "app.apk" }
//
//                    //为了兼容android10,如果需要把文件下载到共享文件夹，可以调用serUri
//                    setUri = {
//                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
//                            val values = ContentValues().apply {
//                                put(MediaStore.MediaColumns.DISPLAY_NAME, "app.apk") //文件名
//                                put(
//                                    MediaStore.MediaColumns.MIME_TYPE,
//                                    response.body()?.contentType().toString()
//                                ) //文件类型
//                                put(
//                                    MediaStore.MediaColumns.RELATIVE_PATH,
//                                    Environment.DIRECTORY_DOWNLOADS
//                                ) //共享文件夹，固定写法
//                            }
//                            requireContext().contentResolver.insert(
//                                MediaStore.Downloads.EXTERNAL_CONTENT_URI,
//                                values
//                            )
//                        } else
//                            Uri.fromFile(File(requireContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)?.absolutePath + File.separator + "app.apk"))
//                    }
//
//                }.startDowload() //调用.startDowload()开启下载
//            }
//        }
    }
}